package org.example.controller;

import org.example.entity.CommonResult;
import org.example.entity.Poem;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.example.service.PoemService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/poem")
public class PoemController {

    @Resource
    PoemService poemService;

    /***
     * 功能：调用poemService中的addPoem，增添诗词。
     * @param poem
     * @return 诗词
     */
    @RequestMapping("/add")
    public CommonResult<Poem> addPoem(@RequestBody Poem poem){
        if(poemService.addPoem(poem)){
            return new CommonResult<Poem>(200,"success",null);
        }
        return new CommonResult<Poem>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的deletePoem，根据poemId删除相应的诗。
     * @param poemId
     * @return 诗词
     */
    @RequestMapping("/delete")
    public CommonResult<Poem> deletePoem(@RequestParam("poemId") Long poemId){
        if(poemService.deletePoem(poemId)){
            return new CommonResult<Poem>(200,"success",null);
        }
        return new CommonResult<Poem>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的updatePoem，更新相应的诗词。
     * @param poem
     * @return 诗词
     */
    @RequestMapping("/update")
    public CommonResult<Poem> updatePoem(@RequestBody Poem poem){
        if(poemService.updatePoem(poem)){
            return new CommonResult<Poem>(200,"success",null);
        }
        return new CommonResult<Poem>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getPoem，根据poemId查询相应的诗词
     * @param poemId
     * @return 诗词
     */
    @RequestMapping("/get")
    public CommonResult<Poem> getPoem(@RequestParam("poemId") Long poemId){
        if(poemService.getPoem(poemId)!=null){
            return new CommonResult<Poem>(200,"success", poemService.getPoem(poemId));
        }
        return new CommonResult<Poem>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getPoemByType，根据type查询相应的诗词集
     * @param type
     * @return 诗词集
     */
    @RequestMapping("/getByType")
    public CommonResult<List<Poem>> getPoemByType(@RequestParam("type") String type,
                                                  @RequestParam("cnt") Integer cnt){
        if(poemService.getPoemByType(type,cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.getPoemByType(type,cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getPoemByWriter，根据writer查询相应的诗词集
     * @param writer
     * @return 诗词集
     */
    @RequestMapping("/getByWriter")
    public CommonResult<List<Poem>> getPoemByWriter(@RequestParam("writer") String writer,
                                                    @RequestParam("cnt") Integer cnt){
        if(poemService.getPoemByWriter(writer,cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.getPoemByWriter(writer,cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getPoemByDynasty，根据dynasty查询相应的诗词集
     * @param dynasty
     * @return 诗词集
     */
    @RequestMapping("/getByDynasty")
    public CommonResult<List<Poem>> getPoemByDynasty(@RequestParam("dynasty") String dynasty,
                                                     @RequestParam("cnt") Integer cnt){
        if(poemService.getPoemByDynasty(dynasty,cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.getPoemByDynasty(dynasty,cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getAllPoem，得到所有的诗词集
     * @param
     * @return 诗词集
     */
    @RequestMapping("/getAll")
    public CommonResult<List<Poem>> getAllPoem(@RequestParam("cnt") Integer cnt){
        if(poemService.getAllPoem(cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.getAllPoem(cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getPoemByTitle，根据标题模糊查询诗词集
     * @param title
     * @return 诗词集
     */
    @RequestMapping("/getByTitle")
    public CommonResult<List<Poem>> getPoemByTitle(@RequestParam("title") String title,
                                                   @RequestParam("cnt") Integer cnt){
        if(poemService.getPoemByTitle(title,cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.getPoemByTitle(title,cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getPoemByPassage，根据内容模糊查询诗词集
     * @param passage
     * @return 诗词集
     */
    @RequestMapping("/getByPassage")
    public CommonResult<List<Poem>> getPoemByPassage(@RequestParam("passage") String passage,
                                                     @RequestParam("cnt") Integer cnt){
        if(poemService.getPoemByPassage(passage,cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.getPoemByPassage(passage,cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的fuzzySearch，根据field匹配标题、内容、体裁、作者、朝代，综合模糊查询诗词集
     * @param field
     * @return 诗词集
     */
    @RequestMapping("/fuzzySearch")
    public CommonResult<List<Poem>> fuzzySearch(@RequestParam("field") String field,
                                                @RequestParam("cnt") Integer cnt) throws Exception {
        if(poemService.fuzzySearch(field,cnt)!=null){
            return new CommonResult<List<Poem>>(200,"success", poemService.fuzzySearch(field,cnt));
        }
        return new CommonResult<List<Poem>>(400,"error",null);
    }

    /***
     * 功能：调用poemService中的getOneByTitle，根据title精确查询一首诗词
     * @param title
     * @return 诗词
     */
    @RequestMapping("/getOneByTitle")
    public CommonResult<Poem> getOneByTitle(@RequestParam("title") String title){
        if(poemService.getOneByTitle(title)!=null){
            return new CommonResult<Poem>(200,"success", poemService.getOneByTitle(title));
        }
        return new CommonResult<Poem>(400,"error",null);
    }
}
